package com.jkzx;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jkzx.view.MyViewPage;
import com.jkzx.view.SyncHorizontalScrollView;

public class NoveltyActivity extends FragmentActivity {

	private int currentIndicatorLeft = 0;
	public static final String ARGUMENTS_NAME = "arg";
	public static String[][] tabTitles = {{ "卫生法律", "行政法规", "部门规章"},{ "传染疾病", "环境卫生", "辐射卫生", "职业卫生"},{ "直属单位", "传染病医院", "疫苗注射网点"},{ "亚洲", "欧洲", "非洲", "南美洲", "北美洲", "大洋洲"},{ "妇幼保健", "疫苗接种", "营养与食品", "健康促进"}};	//标题, "选项4", "选项5"
	public String[] titles={"政策法规","疾控知识","预防机构","旅行健康","生活信息"};
	public static int[] tabId = {1,2,3,4};	//标题, "选项4", "选项5"  
	private ViewPager mViewPager;
	private RadioGroup rg_nav_content;
	private ImageView iv_nav_indicator;
	private SyncHorizontalScrollView mHsv;
	private LayoutInflater mInflater;
	private int indicatorWidth;
	private RelativeLayout rl_nav;
	private ImageView iv_nav_left;
	private ImageView iv_nav_right;
	private TabFragmentPagerAdapter mAdapter;
	private int category;
	private TextView tv_headTitle;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newmessage);
		mViewPager = (MyViewPage) findViewById(R.id.mViewPager);
		rg_nav_content = (RadioGroup) findViewById(R.id.rg_nav_content);
		tv_headTitle = (TextView) findViewById(R.id.tv_headTitle);
		iv_nav_indicator = (ImageView) findViewById(R.id.iv_nav_indicator);
		mHsv = (SyncHorizontalScrollView) findViewById(R.id.mHsv);
		rl_nav = (RelativeLayout) findViewById(R.id.rl_nav);
		category=getIntent().getIntExtra("category", 4);
		initView();
		setListener();
		tv_headTitle.setText(titles[category]);
	}
	private void initView() {
		//获取分辨�?		
		DisplayMetrics dm = new DisplayMetrics();
		//调用getWindowManager() 之后，会取得现有Activity 的Handle ，此�?		
		//getDefaultDisplay() 方法将取得的宽高维度存放于DisplayMetrics 对象中，
		//而取得的宽高维度是以像素为单�?Pixel) ，�?像素”所指的是�?绝对像素”�?非�?相对像素”�?
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		indicatorWidth = dm.widthPixels / 4;

		LayoutParams cursor_Params = iv_nav_indicator.getLayoutParams();
		cursor_Params.width = indicatorWidth;// 初始化滑动下标的�?		iv_nav_indicator.setLayoutParams(cursor_Params);

		mHsv.setSomeParam(rl_nav, iv_nav_left, iv_nav_right, this);

		//获取布局填充�?		
		mInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);

		//另一种方式获�?//		LayoutInflater mInflater = LayoutInflater.from(this);  
		for(int i=0;i<tabTitles[category].length;i++){
			RadioButton rb = (RadioButton) mInflater.inflate(R.layout.nav_radiogroup_item, null);
			rb.setId(i);
			rb.setText(tabTitles[category][i]);
			rb.setLayoutParams(new LayoutParams(indicatorWidth,
					LayoutParams.MATCH_PARENT));
			rg_nav_content.addView(rb);
		}
		mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(mAdapter);
	}
	private void setListener() {
		ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
		iv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				if(rg_nav_content!=null && rg_nav_content.getChildCount()>position){
					((RadioButton)rg_nav_content.getChildAt(position)).performClick();
					mAdapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		rg_nav_content.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {

				if(rg_nav_content.getChildAt(checkedId)!=null){

					TranslateAnimation animation = new TranslateAnimation(
							currentIndicatorLeft ,
							((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft(), 0f, 0f);
					animation.setInterpolator(new LinearInterpolator());
					animation.setDuration(100);
					animation.setFillAfter(true);

					//执行位移动画
					iv_nav_indicator.startAnimation(animation);

					mViewPager.setCurrentItem(checkedId);	//ViewPager 跟随�?�� 切换

					//记录当前 下标的距�?��侧的 距离
					currentIndicatorLeft = ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft();

					mHsv.smoothScrollTo(
							(checkedId > 1 ? ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft() : 0) - ((RadioButton) rg_nav_content.getChildAt(2)).getLeft(), 0);
				}
			}
		});
	}
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {

			super.handleMessage(msg);
			switch (msg.what) {
				case 1:
					mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager());
					mViewPager.setAdapter(mAdapter);
					break;
				default:
					break;
			}
		}

	};

	Fragment ft = null;
	public float mLastMotionX;
	public class TabFragmentPagerAdapter extends FragmentPagerAdapter{


		public TabFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			System.out.println(arg0+"-fr");
			if(arg0==0){
				ft = new LaunchUIFragment2();
				Bundle bundle=new Bundle();
				bundle.putInt("index", 0);
				ft.setArguments(bundle);
			}else
				ft = new LaunchUIFragment();
			Bundle args = new Bundle();
//				args.putString(ARGUMENTS_NAME, tabTitles[category][arg0]);
			args.putInt("index", arg0);
			args.putInt("category", category);
			ft.setArguments(args);
			return ft;
		}

		@Override
		public int getCount() {

			return tabTitles[category].length;
		}
	}


}
